package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.constant.BusinessEnum;
import com.powernode.domain.Category;
import com.powernode.domain.Prod;
import com.powernode.domain.ProdTagReference;
import com.powernode.feign.SearchProductFeign;
import com.powernode.model.Result;
import com.powernode.service.SearchService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @create: 2024/7/24
 * @author: gyhe10
 * @description:
 **/

@Service
public class SearchServiceImpl implements SearchService {

    @Resource
    private SearchProductFeign searchProductFeign;

    /**
     * 根据分组标签id分页查询商品列表
     *
     * @param tagId
     * @param current
     * @param size
     * @return
     */
    @Override
    public Page<Prod> queryProdPageByTagId(Long tagId, Long current, Long size) {

        // 创建最后返回的商品分页对象
        Page<Prod> prodPage = new Page<>(current, size);

        // 远程调用product-service接口：根据分组标签id查询商品与分组标签的关联关系集合
        Result<Page<ProdTagReference>> result = searchProductFeign.getProdTagReferencePageByTagId(tagId, current, size);
        // 判断远程调用是否成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(result.getCode())) {
            throw new RuntimeException("远程调用product-service接口，根据分组标签id分页查询商品与分组标签关系集合失败");
        }
        // 获取商品与分组标签的关联关系集合
        Page<ProdTagReference> prodTagReferencePage = result.getData();
        List<ProdTagReference> prodTagReferenceList = prodTagReferencePage.getRecords();

        // 判断商品与分组标签的关联关系集合是否为空
        if (CollectionUtil.isEmpty(prodTagReferenceList) || prodTagReferenceList.size() == 0) {
            return prodPage;
        }

        // 获取商品id集合
        List<Long> prodIdList = prodTagReferenceList.stream().map(ProdTagReference::getProdId).collect(Collectors.toList());

        // 远程调用product-service接口：根据商品id集合查询商品列表
        Result<List<Prod>> prodListResult = searchProductFeign.getProdListByIds(prodIdList);
        // 判断远程调用是否成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(prodListResult.getCode())) {
            throw new RuntimeException("远程调用product-service接口，根据商品id集合查询商品列表失败");
        }
        List<Prod> prodList = prodListResult.getData();

        // 封装商品分页对象
        prodPage.setRecords(prodList);
        prodPage.setTotal(prodTagReferencePage.getTotal());
        prodPage.setPages(prodTagReferencePage.getPages());

        return prodPage;
    }

    /**
     * 根据商品类目id查询商品集合
     * 1. 当前商品类目id为一级商品类目id
     * 2. 查询商品集合应该是一级商品类目下的所有商品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Prod> queryWxProdListByCategoryId(Long categoryId) {

        // 创建所有类目id集合
        List<Long> allCategoryIdList = CollectionUtil.newArrayList();
        allCategoryIdList.add(categoryId);

        // 远程调用product-service接口：根据商品一级类目id查询子类目集合
        Result<List<Category>> categoryListResult = searchProductFeign.getCategoryListByParentId(categoryId);
        // 判断远程调用是否成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(categoryListResult.getCode())) {
            throw new RuntimeException("远程调用product-service接口，根据商品一级类目id查询子类目集合失败");
        }
        // 获取商品一级类目的子类目集合
        List<Category> categoryList = categoryListResult.getData();
        // 判断商品一级类目的子类目集合是否为空
        if (CollectionUtil.isNotEmpty(categoryList) && categoryList.size() > 0) {
            // 不为空，从商品一级类目的子类目集合中获取类目id集合
            List<Long> categoryIdList = categoryList.stream().map(Category::getCategoryId).collect(Collectors.toList());
            allCategoryIdList.addAll(categoryIdList);
        }

        // 远程调用product-service接口：根据类目id集合查询商品集合
        Result<List<Prod>> prodListResult = searchProductFeign.getProdListByCategoryIds(allCategoryIdList);
        // 判断远程调用是否成功
        if (BusinessEnum.OPERATION_FAIL.getCode().equals(prodListResult.getCode())) {
            throw new RuntimeException("远程调用product-service接口，根据类目id集合查询商品集合失败");
        }
        return prodListResult.getData();
    }
}
